package com.controller;

import com.model.*;
import com.response.Response;
import com.service.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/api/reviews")
public class ReviewsController {
    
    @Resource
    private OrdersService ordersService;
    
    @Resource
    private StudentsService studentsService;
    
    @Resource
    private WorksService worksService;
    
    // 获取用户收到的评价列表
    @RequestMapping(value = "/list")
    @ResponseBody
    public Response getUserReviews(HttpServletRequest req) throws Exception {
        try {
            String sno = req.getParameter("sno");
            
            if (sno == null || sno.isEmpty()) {
                return Response.error(201, "用户ID不能为空");
            }
            
            // 创建查询条件 - 查找该用户发布的任务
            Works works = new Works();
            works.setCondition(" and a.wid in(select wid from orders where sno='"+sno+"' and flag='已完成' and sc is not null)");
            List<Works> worksList = worksService.queryWorksList(works, null);
            
            List<Map<String, Object>> reviewsList = new ArrayList<>();
            
            // 遍历该用户发布的所有任务
            for (Works work : worksList) {
                // 查找该任务的订单
                Orders orderQuery = new Orders();
                orderQuery.setWid(work.getWid());
                orderQuery.setFlag("已完成"); // 只查找已完成的订单
                List<Orders> ordersList = ordersService.queryOrdersList(orderQuery, null);
                
                // 遍历订单，找出有评价的订单
                for (Orders order : ordersList) {
                    if (order.getSc() != null && order.getPmemo() != null && !order.getPmemo().isEmpty()) {
                        // 获取接单人信息
                        Students reviewer = studentsService.queryStudentsById(work.getSno());
                        
                        Map<String, Object> reviewMap = new HashMap<>();
                        reviewMap.put("orderNo", order.getNo());
                        reviewMap.put("taskTitle", work.getWtitle());
                        reviewMap.put("reviewerSno", reviewer.getSno());
                        reviewMap.put("reviewerName", reviewer.getSname());
                        reviewMap.put("reviewerPhoto", reviewer.getPhoto());
                        reviewMap.put("rating", order.getSc());
                        reviewMap.put("comment", order.getPmemo());
                        reviewMap.put("reviewTime", order.getPtime());
                        
                        reviewsList.add(reviewMap);
                    }
                }
            }
            
            return Response.success(reviewsList);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error(500, "服务器错误: " + e.getMessage());
        }
    }
}
